# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.10.11
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ilmbase"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-partio"
         "${MINGW_PACKAGE_PREFIX}-pugixml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs' '!emptydirs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenShadingLanguage/archive/Release-${pkgver}.tar.gz
        0001-install-dirs.patch
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0005-Fix-cast-from-void-ptr.patch
        0007-Fix-bad-casts-with-intptr_t.patch
        0010-macros-undef-mingw.patch
        0012-isatty-mingw.patch
        0013-disable-assert-partio.patch
        0014-fix-testshade-library-name.patch)
sha256sums=('1622582de42f6458fff44a0f85bf90ed56b50d4ec97b7637c14ce3b5bdb666b5'
            'e3f6ad09619d122ada129cbc77d7778751abfecef9ac7e0a60d9550c811df643'
            '70d4ce3c69ed072eee0d2bb84cecea5ff9ff35e258a24a7a78ec94e4b626e493'
            '355120807bab0e7c0ba08d9831ee8e64f4faaedc07a769bad28aca8732880d18'
            '541cc1eb0c506e253de2f26af37dc6e35de891a940906c6e33a2f4774a4a844e'
            'b6ac561e4f450324ff4d60f363de9f266dd4d01d43bc1ec0ee1190cfb7f35c2c'
            'd6fe2f65d219b124f9155a7c362cffb085134acd0882786870d72b519994f4cc'
            '7d9de868949b83d20b1807a8eb6d21402a4fc141c85cd1ca6e74a326b79be284'
            '8094cd52922f20523a74bb0d249dcf74c1c7ec7b5b25cbef0217955986642558'
            'c506a905b5549356d1674924be3ddc23d4173ddf3c562c077d076cd9c52c0756')

prepare() {
  cd ${srcdir}/OpenShadingLanguage-Release-${pkgver}
  #patch -p1 -i ${srcdir}/0001-install-dirs.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
  patch -p1 -i ${srcdir}/0010-macros-undef-mingw.patch
  patch -p1 -i ${srcdir}/0012-isatty-mingw.patch
  patch -p1 -i ${srcdir}/0013-disable-assert-partio.patch
  patch -p1 -i ${srcdir}/0014-fix-testshade-library-name.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  CFLAGS+=" -Wno-attributes -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -Wno-attributes -DTINYFORMAT_ALLOW_WCHAR_STRINGS -D_USE_MATH_DEFINES"

  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSTOP_ON_WARNING=OFF \
    -DLLVM_STATIC=1 \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DCMAKE_CXX_STANDARD=14 \
    ${extra_config} \
    ../OpenShadingLanguage-Release-${pkgver}

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH=${srcdir}/build-${MINGW_CHOST}/src/liboslcomp:"${PATH}" make #VERBOSE=1
}

package() {
  cd ${srcdir}//build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  
  install -Dm644 "${srcdir}/OpenShadingLanguage-Release-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
